package com.exam.master.model.main.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.exam.commonbiz.util.CommonCallBack;
import com.exam.commonbiz.util.ScreenUtil;
import com.exam.commonbiz.util.ToastUtil;
import com.exam.master.R;
import com.exam.master.WXApplication;

public class BindPhoneDialog {

    public void show(Context context, CommonCallBack commonCallBack) {
        LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.dialog_layout_bind_phone, null);
        EditText edtContent = layout.findViewById(R.id.edt_phone);
        Dialog dialog = new Dialog(context, R.style.BottomSheetDialog);
        dialog.setContentView(layout);
        dialog.show();
        setPosition(dialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);

        layout.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        layout.findViewById(R.id.tv_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String code = edtContent.getText().toString();
                if (TextUtils.isEmpty(code)){
                    ToastUtil.showLongToastCenter("手机号不能为空");
                    return;
                }
                dialog.dismiss();
                if (commonCallBack != null) {
                    commonCallBack.callback(0, code);
                }
            }
        });
    }

    public void setPosition(Dialog dialog) {

        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.CENTER);

        lp.width = (int) (ScreenUtil.getScreenWidth(WXApplication.getInstance()) * 0.8);
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
    }
}
